/**
 * 自定义函数
 * */


//列表
function getIndex(){
    var formData = {
        aid: getAid(),
        pageStart: pages.start,
        pageLimit: pages.limit
    };
    console.log(formData);
    $.ajax({
        type: apis.tmpIndex.method,
        url: apis.tmpIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code != 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>' + d[i].scene_name + '</td>';
        h += '<td>' + d[i].uptime + '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" onclick="lay_edit(\'编辑\',\'tmp1-edit.html\', ' + d[i].id + ');">';
        h += '编辑<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//编辑
function lay_add(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,300);
}


//编辑
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//获取详情
function getOne(v){
    if (!v) { return }
    var formData = {
        id: v,
    };
    console.log(formData);
    //return
    $.ajax({
        type: apis.tmp1One.method,
        url: apis.tmp1One.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data;
            var imgs = d.imgs;
            var attrs = d.attrs;
            $('#title')[0].value = d.title;
            $('#tmp_code').html(d.tmp_code);
            $('#ucode').html(d.ucode);
            $('#qrcode').html(d.code);
            var h = '', i;
            h = '';
            for (i = 0; i < imgs.length; i ++) {
                h += '<img src="' + imgs[i].link + '"/>';
            }
            $('#bigShow').html(h);
            h = '';
            h += '<style type="text/css">.attrGroup { margin: 5px; } .attrGroup input { margin: 0 2px; } </style>';
            for (i = 0; i < attrs.length; i ++) {
                h += '<div class="attrGroup">';
                h += '<input type="text" class="input-text" id="attr-key-' +  attrs[i].id + '" style="width: 30%;" placeholder="属性名" value="' +  attrs[i].attr_name + '"/>';
                h += '<input type="text" class="input-text" id="attr-val-' +  attrs[i].id + '" style="width: 30%;" placeholder="属性值" value="' +  attrs[i].val + '"/>';
                h += '<a href="javascript:;" class="attrBtn" onclick="attrModify(' + attrs[i].id + ');">保存修改</a>';
                h += '<input type="hidden" id="tmpid-' + attrs[i].id + '" value="' + attrs[i].tmp_id + '"/>';
                h += '</div>';
            }
            h += '<input type="hidden" id="tmpid-0" value="' + v + '"/>';
            $('#attrList').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//修改属性
function attrModify(v){
    var formData = {
        id: v,
        tmp_id: $('#tmpid-' + v).val(),
        attrKey: $('#attr-key-' + v).val(),
        attrVal: $('#attr-val-' + v).val(),
    };
    $.ajax({
        type: apis.tmp1AttrModify.method,
        url: apis.tmp1AttrModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function () {
                location.reload();
            }, 1000);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//抓图
function uploadImg() {
    var fileObj = $('#bigUpload')[0], reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        //showImg({img: res.target.result});
        var formData = {
            id: id, img: res.target.result,
        };
        $.ajax({
            type: apis.tmp1UploadImg.method,
            url: apis.tmp1UploadImg.url,
            data: formData,
            success: function (res) {
                console.log(res);
                if (res.code != 0) {
                    msgPop({msg: res.msg}); return
                }
                msgPop({msg: '操作成功'});
                setTimeout(function () {
                    location.reload();
                }, 1000);
            },
            error: function () {
                console.log('网络错误');
            }
        });
    }
}


////显示图片
//function showImg(e){
//    $('#imgs').html(imgHtml());
//    $('#uploadOut').html(uploadHtml());
//}


//添加物品
function add(){
    var formData = {
        title: $('#title').val(),
    };
    if (formData.title == '') {
        msgPop({msg: '物品名必须的'}); return
    }
    console.log(formData);
    //return
    $.ajax({
        type: apis.tmp1Add.method,
        url: apis.tmp1Add.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '添加成功'});
            setTimeout(function () {
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//更新物品
function modify(){
    var formData = {
        id: id,
        title: $('#title').val(),
    };
    if (formData.title == '') {
        msgPop({msg: '物品名必须的'}); return
    }
    console.log(formData);
    //return
    $.ajax({
        type: apis.tmp1Modify.method,
        url: apis.tmp1Modify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '修改成功'});
            setTimeout(function () {
                location.reload();
            }, 1000);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}